<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/26
 * Time: 17:10
 */

namespace app\course\model;


use think\Db;
use think\Model;
use tree\Tree;



class CourseModel extends Model
{
    // 开启自动写入时间戳字段
//    protected $autoWriteTimestamp = true;

    /**
     * 关联 user表
     * @return $this
     */
    public  function user()
    {
        return $this->belongsTo("UserModel",'user_id')->setEagerlyType(1);

    }

    public function courseTree($selectId = 0, $currentCid = 0)
    {
        $where = [];
        if (!empty($currentCid)) {
            $where['id'] = ['neq', $currentCid];
        }
        $categories = $this->where($where)->select()->toArray();

        $tree       = new Tree();
        $tree->icon = ['&nbsp;&nbsp;│', '&nbsp;&nbsp;├─', '&nbsp;&nbsp;└─'];
        $tree->nbsp = '&nbsp;&nbsp;';

        $newCategories = [];
        foreach ($categories as $item) {
            $item['selected'] = $selectId == $item['id'] ? "selected" : "";
            array_push($newCategories, $item);
        }
        $tree->init($newCategories);
        $str     = '<option value=\"{$id}\" {$selected}>{$spacer}{$name}</option>';
        $treeStr = $tree->getTree(0, $str);
        return $treeStr;
    }


    /**
     * @param $shop_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getAllCourse($shop_id)
    {
        if(empty($shop_id)){
            //默认店铺
            $shop_id = 1;
        }

        $data = $this->where('shop_id', '=', $shop_id)//多店铺
        ->where('delete_time', '=', 0)//未删除
        ->select();
        return $data;
    }


}